#!/usr/bin/env python

# This script exchanges every occurence of four whitespaces with tabs

import sys, os

if len(sys.argv) > 0:
	if not os.path.exists(sys.argv[1]):
		print "File "+ sys.argv[1]+" does not exist."
		sys.exit()

file = sys.argv[1]
try:
	f = open(file)
	lines = f.readlines()
finally:
	f.close()

new = []
for i in lines:
	temp= i
	if temp.lstrip() == '':		#empty lines containing only of whitespaces or tabs are deleted
		new.append('\n')
		continue
		
	c = 0
	while temp[:4] == 4*' ':	# lines starting with 4 whitespaces 
		c += 1
		temp = temp[4:]

	new.append(c * "\t" + temp.lstrip(' '))	#lstrip deletes all whitespaces which are still in the string 

f = open(file, 'w')
f.writelines(new)
f.close()
